package com.example.generator.service.impl;

import com.example.generator.config.PropertiesConfig;
import com.example.generator.service.BaseService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Map;

/**
 * 生成配置文件
 *
 * @author smallNorth_Lee
 * @date 2022/1/11
 */
@Service
public class GeneratorYmlImpl extends BaseService {

    private final PropertiesConfig propertiesConfig;

    public GeneratorYmlImpl(PropertiesConfig propertiesConfig) {
        this.propertiesConfig = propertiesConfig;
    }


    /**
     * 生成配置文件
     *
     * @param objectMap       项目配置信息
     * @param projectRootPath 项目文件路径
     */
    public void doGeneration(Map<String, Object> objectMap, String projectRootPath) {

        //静态资源
        super.writeFile(new File(projectRootPath + "static"), "", "");
        super.writeFile(new File(projectRootPath + "db/migration/V1.1__init_create_user.sql"), "V1.1__init_create_user.sql.ftl", objectMap);
        //静态模版
        super.writeFile(new File(projectRootPath + "templates"), "", "");
        objectMap.put("url",propertiesConfig.getUrl());
        objectMap.put("username",propertiesConfig.getUsername());
        objectMap.put("password",propertiesConfig.getPassword());
        super.writeFile(new File(projectRootPath, "application.yml"), "applicationYml.ftl", objectMap);
    }


}
